package com.yyaccp.appinfo.controller;

import com.yyaccp.appinfo.domain.DevUser;
import com.yyaccp.appinfo.exception.LoginException;
import com.yyaccp.appinfo.service.DevUserService;
import com.yyaccp.appinfo.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/devUsers")
public class DevUserController {
    @Autowired
    private DevUserService devUserService;

    @GetMapping("getloginuser")
    public AjaxResult getLoginUser(HttpSession session){
        DevUser devUser = (DevUser) session.getAttribute("loginUser");
        if(devUser == null){
            return AjaxResult.error();
        }
        return AjaxResult.success(devUser);
    }
    @PostMapping("/login")
    public AjaxResult login(DevUser user, HttpSession session){
        System.out.println(user.getDevCode());
        try{
            DevUser devUser = devUserService.login(user);
            devUser.setDevPassword("******");
            session.setAttribute("loginUser",devUser);
            return AjaxResult.success(devUser);
        }catch(LoginException ex){
            return AjaxResult.error(ex.getMessage());
        }
    }
}
